<template>
	<view>
		<view class="outer">
			<view class="inputPart">
				<uni-easyinput class="uni-mt-5 input1" trim="all" v-model="user.tel" placeholder="手机号">
				</uni-easyinput>
				<uni-easyinput class="uni-mt-5 input2" v-model="user.password" placeholder="密码"></uni-easyinput>
			</view>
			<view class="btnPart">
				<button class="btn" type="primary" @click="login">Sign up</button>
			</view>
			<view class="otherPart">
				<view class="other1">-----------------</view>
				<view class="other2">未有账号？</view>
				<view class="other3">--------------</view>
			</view>
			<view class="loginWayPart" @click.native="registerPage">
				<uni-icons type="contact" color="#fff" size="45"></uni-icons>
				<view class="">注册</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					tel: "",
					password: ""
				}
			};
		},
		methods: {
			login() {
				if (this.user.tel == "" || this.user.password == "") {
					uni.showToast({
						title: "请输入完整信息",
						icon: 'error'
					})
					return;
				}
				uni.request({
					method: 'POST',
					url: "api/appuser/login",
					data: {
						tel: this.user.tel,
						password: this.user.password
					},
					success: (res) => {
						if (res.data.code == 200) {
							uni.showToast({
								title: "登录成功",
								icon: 'success'
							})
							// console.log(res.data.data.userInfo);
							uni.setStorageSync('authority',res.data.data.jwt);
							uni.setStorageSync('userInfo',res.data.data.userInfo);
							uni.reLaunch({
								url:"/pages/index/index"
							})
						} else if (res.data.code == 2004) {
							uni.showToast({
								title: "账号不存在，请先注册",
								icon: 'error'
							})
						} else if (res.data.code == 2005) {
							uni.showToast({
								title: "账号或密码错误",
								icon: 'error'
							})
						}
					}
				})
			},
			registerPage() {
				uni.reLaunch({
					url: "/pages/register/register",
				})
			}
		}
	}
</script>

<style lang="scss">
	.inputPart {
		margin-top: 150rpx;

		.input1 {
			width: 550rpx;
			margin: 0 auto;
		}

		.input2 {
			width: 550rpx;
			margin: 30rpx auto;
		}
	}

	.btnPart {
		margin-top: 70rpx;

		.btn {
			width: 550rpx;
			margin: 30rpx auto;
		}
	}

	.otherPart {
		width: 550rpx;
		margin: 0 auto;
		margin-top: 600rpx;

		.other1 {
			float: left;
		}

		.other2 {
			float: left;
		}

		.other3 {
			// float: left;
		}
	}

	.loginWayPart {
		width: 140rpx;
		height: 140rpx;
		margin: 0 auto;
		margin-top: 40rpx;
		background-color: gray;
		text-align: center;
		border-radius: 100rpx;
		background-color: #020202;
		color: #fff;
	}
</style>